package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.AliYunUtils;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.facebook.drawee.view.SimpleDraweeView;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.BigPhotoBean;
import rxbus_base.RxBus;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/25.
 */

public class BigPhotoAndDescribeActivity extends BaseActivity {
    @InjectView(R.id.tv_title)
    TextView titleTv;
    @InjectView(R.id.fresco_big_photo)
    SimpleDraweeView bigPhotoFresco;
    @InjectView(R.id.tv_describe)
    TextView describeTv;
    private String title;
    private String photo;
    private String describe;
    private String classId;
    private String sourceId;
    private String fileName;
    private boolean isSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_big_photo_and_describe);
        ButterKnife.inject(this);
        initViews();
        if (isSave) {
            getData();
            describeTv.setVisibility(View.VISIBLE);
        } else {
            describeTv.setVisibility(View.GONE);
        }
    }

    private void getData() {
        HashMap<String, Object> build = new MapUtil()
                .put("id",  CustomSharedPreferences.getString(BigPhotoAndDescribeActivity.this, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf()))
                .put("class_id", classId)
                .put("attr_id", sourceId)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(BigPhotoAndDescribeActivity.this, build).create(IApiActivityRequest.class).getBigPhoto()
                , new BaseSubscriber<TokenBean<BigPhotoBean>>(BigPhotoAndDescribeActivity.this) {
                    @Override
                    public void onSuccess(TokenBean<BigPhotoBean> o) {
                        if (o.getData() != null && o.getData().getOption() != null && o.getData().getOption().size() > 0) {
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < o.getData().getOption().size(); i++) {
                                if (i != o.getData().getOption().size() - 1) {
                                    describeTv.append(o.getData().getOption().get(i).getKey() + " : " + o.getData().getOption().get(i).getValue() + "\n");
                                } else {
                                    describeTv.append(o.getData().getOption().get(i).getKey() + " : " + o.getData().getOption().get(i).getValue());
                                }
                            }
                        }

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    private void initViews() {
//        title = getIntent().getStringExtra("title");
//        describe = getIntent().getStringExtra("describe");
        photo = getIntent().getStringExtra("photoUrl");
        classId = getIntent().getStringExtra("classId");
        sourceId = getIntent().getStringExtra("sourceId");
        fileName = getIntent().getStringExtra("fileName");
        isSave = getIntent().getBooleanExtra("isSave", false);
        if (!TextUtils.isEmpty(title)) {
            titleTv.setText(title);
        }
        if (photo.contains("http")) {
            bigPhotoFresco.setImageURI(photo);
        } else {
            bigPhotoFresco.setImageURI("file://" + photo);
        }
        bigPhotoFresco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(BigPhotoAndDescribeActivity.this, ScaleBigPhotoActivity.class);
                if (!TextUtils.isEmpty(photo)) {
                    if (photo.contains("http")) {
                        intent.putExtra("imgUrls", photo);
                    } else {
                        intent.putExtra("imgUrls", "file://" + photo);
                    }
                }
                startIntent(intent, false);
            }
        });
    }

    @OnClick({R.id.iv_return, R.id.tv_save_info})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.tv_save_info:
                    deleteALiYunFile();
                    deleteServiceFile();
                break;
        }
    }

    private void deleteServiceFile() {
        HashMap<String, Object> build = new MapUtil()
                .put("id",  CustomSharedPreferences.getString(this, KeyBean.CARID.valueOf(),KeyBean.FILE_NAME.valueOf()))
                .put("class_id", classId)
                .put("attr_id", sourceId)
                .put("img_info", fileName)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).deleteFile()
                , new BaseSubscriber<TokenBean>(this) {
                    @Override
                    public void onSuccess(TokenBean o) {
                        RxBus.getInstance().sendData(sourceId,null);
                        finish();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        RxBus.getInstance().sendData(sourceId,null);
                        finish();
                    }
                });
    }

    private void deleteALiYunFile() {
        AliYunUtils.deleteFile(this, fileName);
    }
}
